package com.cap.bts.crmdp.dealer.coupon.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cap.bts.crmdp.dealer.coupon.constants.CouponConstants;
import com.cap.bts.crmdp.dealer.coupon.dao.ICouponDao;
import com.cap.bts.crmdp.dealer.coupon.dao.ICouponEligibilityDao;
import com.cap.bts.crmdp.dealer.coupon.domain.CouponEligibility;
import com.cap.bts.crmdp.dealer.coupon.dto.CouponEligibilityDto;
import com.cap.bts.crmdp.dealer.coupon.service.ICouponEligibilityService;
import com.cap.bts.crmdp.dealer.masterdata.dao.IMaintainDealerDao;
import com.cap.bts.crmdp.dealer.masterdata.domain.MasterDataDealerInfo;
import com.cap.bts.framework.codemapping.dao.ICodeMappingDao;
import com.cap.bts.framework.codemapping.domain.CodeInfo;
import com.cap.bts.framework.common.exception.AppException;
@Transactional(rollbackFor = AppException.class)
@Service
public class CouponEligibilityServiceImpl implements ICouponEligibilityService {
	
	@Autowired
	ICouponEligibilityDao couponEligibilityDaoImpl;
	@Autowired
	ICodeMappingDao codeMappingDao;
	@Autowired
	IMaintainDealerDao dealerDao;
	@Autowired
	ICouponDao couponDao;

	/**
     * 查询套餐的适用范围信息
     * @see com.cap.bts.crmdp.dealer.meal.service.IMealManageService#getPackageEligibilityList(com.cap.bts.crmdp.dealer.meal.dto.PackageEligibilityDto)
     */
    @Override
    public List<CouponEligibility> getCouponEligibilityList(CouponEligibilityDto packageEligibilityDto)throws AppException {
        return couponEligibilityDaoImpl.getCouponEligibilityList(packageEligibilityDto);
        	
    }

    @Override
    public void save(CouponEligibility couponEligibility) throws AppException {
    	
    	if(couponEligibility.getId() == null){            
            couponEligibilityDaoImpl.save(couponEligibility);
        }else{
        	CouponEligibility temp = couponEligibilityDaoImpl.findById(couponEligibility.getId());
        	
            temp.setTicketDefId(couponEligibility.getTicketDefId());
            temp.setDealerProperty(couponEligibility.getDealerProperty());
            temp.setPropertyValue(couponEligibility.getPropertyValue());
            temp.setCodeType(couponEligibility.getCodeType());
        }
    }

    /**
     * 删除套餐的适用范围信息
     * @see com.cap.bts.crmdp.dealer.meal.service.IMealManageService#removePackageEligibilityInfo(com.cap.bts.crmdp.dealer.meal.dto.PackageEligibilityDto)
     */
    @Override
    public boolean removeCouponEligibilityInfo(Long  id)
                                                                                            throws AppException {
        if (couponEligibilityDaoImpl.removeCouponEligibilityInfo(id)) {
            return true;
        }
        return false;
        	
    }
    
    
    public CouponEligibility get(Long  id) throws AppException{
    	
    	return couponEligibilityDaoImpl.findById(id);
    }

    /**
     * 根据套餐ID,Dealer_property,Property_value查询套餐的适用范围
     * @see com.cap.bts.crmdp.dealer.meal.service.IMealManageService#getPackageEligibilityInfoByPackageDefId(com.cap.bts.crmdp.dealer.meal.dto.PackageEligibilityDto)
     */
    @Override
    public CouponEligibility getCouponEligibilityInfoByCouponDefId(CouponEligibilityDto dto)
                                                                                                       throws AppException {
        return couponEligibilityDaoImpl.getCouponEligibilityInfoByCouponDefId(dto);
        	
    }

	@Override
	public boolean checkeApplicableByDealerIdAndTicketId(Long dealerId, Long ticketId) throws AppException {
		//获取门店信息
	   MasterDataDealerInfo dealerInfo = dealerDao.getDealerByDealerNum(dealerId + "");

 	   //构造优先级的map
        Map<String,String> priority = new HashMap<String,String>();
        List<CodeInfo> priorityList = codeMappingDao.getCodeInfoByCodeType(CouponConstants.DEALERPROPERTY);
        if(priorityList != null && !priorityList.isEmpty()){
            for(int k=0; k<priorityList.size(); k++){
                priority.put(priorityList.get(k).getCode().substring(2), priorityList.get(k).getCode());
            }
        }
        
        if(priority != null && !priority.isEmpty()){
     	   List<CouponEligibility> packageEligibilityList = couponEligibilityDaoImpl.getCouponEligibilityByCouponDefId(ticketId);
     	   
     	   if(packageEligibilityList != null && !packageEligibilityList.isEmpty()){
     		   for(int j=0; j<packageEligibilityList.size(); j++){
     			  if(priorityList != null && !priorityList.isEmpty()){
   	               for(int k=0; k<priorityList.size(); k++){
   	            	   if(priorityList.get(k).getCode().equals(packageEligibilityList.get(j).getDealerProperty())){
           				   if(packageEligibilityList.get(j).getPropertyValue().equals(dealerInfo.getRegionCode())){
           					   return true;
           				   }
           			   }
   	               }
   	           }
     			   
     		   }
     	   }
        }
        
		return true;
	}

}
